package expressToAltaRica;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ExpZGen {


	/**
	 * the main methode
	 * @param args : list of the files's path to compress
	 */

	public static void main(String[] args){
		final int BUFFER = 2048;
		byte data[] = new byte[BUFFER];
		try {
			FileOutputStream dest = new FileOutputStream("export.exp");
			BufferedOutputStream buff = new BufferedOutputStream(dest);
			ZipOutputStream out = new ZipOutputStream(buff);
			out.setMethod(ZipOutputStream.DEFLATED);
			out.setLevel(9);
			for(int i=0; i<args.length; i++) {
				File ff=new File(args[i]);
				if (ff.isDirectory()){
					File[] ffs=ff.listFiles();
					if(ffs!=null && ffs.length>0){
						for (int j=0;j<ffs.length;j++){
							System.out.println("files found "+ffs[j]);
						}
						String nameDir=ff.getName();
						for (int j=0;j<ffs.length;j++){
							File ficone=ffs[j];
//							if(ficone.isDirectory()){
//								System.out.println("oki "+ficone.getName());
//							}
							File[] ficones=ficone.listFiles();
							for(int u=0;u<ficones.length;u++){
								FileInputStream fi = new FileInputStream(ficones[u]);
								BufferedInputStream buffi = new BufferedInputStream(fi, BUFFER);
								String nameFile=ficones[u].getName();
								String name=ficone.getName();
								ZipEntry entry = new ZipEntry(nameDir+"/"+name+"/"+nameFile);
								out.putNextEntry(entry);
								int count;
								while((count = buffi.read(data, 0, BUFFER)) != -1) {
									out.write(data, 0, count);
								}
								out.closeEntry();
								buffi.close();
							}
						}
					}
				}else{
					FileInputStream fi = new FileInputStream(new File(args[i]));
					BufferedInputStream buffi = new BufferedInputStream(fi, BUFFER);
					String nameFile=new File(args[i]).getName();
					ZipEntry entry = new ZipEntry(nameFile);
					out.putNextEntry(entry);
					int count;
					while((count = buffi.read(data, 0, BUFFER)) != -1) {
						out.write(data, 0, count);
					}
					out.closeEntry();
					buffi.close();
				}
			}
			out.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//transform the .zip file en .exp file


	}


}

